package com.mashibing.service;

import com.mashibing.constant.CommonStatusEnum;
import com.mashibing.constant.TokenConstantsConfiguration;
import com.mashibing.dto.ResponseResult;
import com.mashibing.dto.TokenResult;
import com.mashibing.util.JwtUtils;
import com.mashibing.util.RedisPrefixUtils;
import io.netty.util.internal.StringUtil;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class TokenService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 刷新token
     * 1、解析token 获取payLoad部分
     * 2、生成redis的tokenKey  从redis中获取刷新token从而验证有效性
     * 3、有效，根据payLoad数据生成新token
     * @param refreshToken
     * @return
     */
    public ResponseResult refreshToken(String refreshToken) {
        // 1、解析token 获取payLoad部分
        TokenResult tokenResult = JwtUtils.checkToken(refreshToken);
        String tokenType = tokenResult.getTokenType();
        String identity = tokenResult.getIdentity();
        String phone = tokenResult.getPhone();

        // 2、生成redis的tokenKey  从redis中获取刷新token从而验证有效性
        String refreshTokenKey = RedisPrefixUtils.generateTokenKey(phone, identity, tokenType);
        String redisRefreshToken = stringRedisTemplate.opsForValue().get(refreshTokenKey);

        // 如果redis中token为空 或 传来的token和redis中不相等
        if (StringUtils.isBlank(redisRefreshToken) || !refreshToken.trim().equals(redisRefreshToken.trim())) {
            return ResponseResult.fail(CommonStatusEnum.TOKEN_ERROR.getCode(),CommonStatusEnum.TOKEN_ERROR.getMessage());
        }

        // 生成访问token
        String generateAccessTokenKey = RedisPrefixUtils.generateTokenKey(phone, identity, TokenConstantsConfiguration.ACCESS_TOKEN_TYPE);
        String generateAccessToken = JwtUtils.generatorToken(phone, identity, TokenConstantsConfiguration.ACCESS_TOKEN_TYPE);
        // 生产环境
        stringRedisTemplate.opsForValue().set(generateAccessTokenKey,generateAccessToken,30,TimeUnit.DAYS);
       // 测试环境
//        stringRedisTemplate.opsForValue().set(generateAccessTokenKey,generateAccessToken,20,TimeUnit.SECONDS);


        // 生成刷新token
        String generateRefreshTokenKey = RedisPrefixUtils.generateTokenKey(phone, identity, TokenConstantsConfiguration.REFRESH_TOKEN_TYPE);
        String generateRefreshToken = JwtUtils.generatorToken(phone, identity, TokenConstantsConfiguration.REFRESH_TOKEN_TYPE);
        // 生产环境
        stringRedisTemplate.opsForValue().set(generateRefreshTokenKey,generateRefreshToken,31, TimeUnit.DAYS);
        // 测试环境
//        stringRedisTemplate.opsForValue().set(generateRefreshTokenKey,generateRefreshToken,60, TimeUnit.SECONDS);

        JSONObject data = new JSONObject();
        data.put("accessToken",generateRefreshToken);
        data.put("refreshToken",generateRefreshToken);
        return ResponseResult.success(data);

    }
}
